#include "nr_device_type.h"
#include "nr_device.h"

nr_device_type::nr_device_type()
    : m_id(NR_INVALID_ID)
    , m_create_device_fun(NULL)
    , m_free_device_fun(NULL)
{
}

nr_device_type::~nr_device_type()
{
}

nr_error_t nr_device_type::load_module()
{
    // Load the module library and the symbols
    QLibrary lib(module());
    
    if (!lib.load())
        return NR_ERROR_LOAD_MODULE_FAILED;

    m_create_device_fun = (nr_create_device_fun)lib.resolve("nr_create_device");
    if (!m_create_device_fun)
        return NR_ERROR_RESOLVE_SYMBOL_FAILED;
    
    m_free_device_fun = (nr_free_device_fun)lib.resolve("nr_free_device");
    if (!m_free_device_fun)
        return NR_ERROR_RESOLVE_SYMBOL_FAILED;

    return NR_ERROR_NONE;
}

nr_device* nr_device_type::create_device()
{
    nr_device* device = NULL;
    if (m_create_device_fun != NULL)
    {
        if (NR_FAILED(m_create_device_fun(id(), (void**)&device)))
        {
            return NULL;
        }
    }
    
    return device;
}

void nr_device_type::destroy_device(nr_device* device)
{
    if (m_free_device_fun != NULL)
    {
        m_free_device_fun(device);
    }
}
